﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenCompiler.Scanning.Scanners;

namespace OpenCompiler.Scanning.Tests
{
	public static class TokenTestHelper
	{
		public static List<Token> Tokenize(string input)
		{
			var scanner = new Scanner(Constants.FakeFileName);
			return scanner.Tokenize(input);
		}

		public static List<Token> TokenizeWithoutEOF(string input)
		{
			var scanner = new Scanner(Constants.FakeFileName);
			var tokens = scanner.Tokenize(input);
			var strippedTokens = from t in tokens
			                     where !t.ActualToken.Equals(Formatting.EndOfFile)
			                     select t;
			return strippedTokens.ToList();
		}

		/// <summary>
		/// Runs all operands and test if the operand is tokenized
		/// </summary>
		/// <param name="format">Input string format - must contain {0} parameter that is replaced with the operand</param>
		/// <param name="expectedTokens"></param>
		/// <param name="expectedTokenIndex"></param>
		public static void TestForTokenBasedOnValuesInEnum<T,S>(string format, int expectedTokens, int expectedTokenIndex)
			where S:StringMatchTokenScanner, new()
		{
			var scanner = new S();
			var operands = EnumHelper.GetValues<T>();
			foreach (var token in operands)
			{
				var operandString = scanner.ValidGrammar[token];
				var formattedSourceCode = string.Format(format, operandString);
				var tokens = TokenTestHelper.TokenizeWithoutEOF(formattedSourceCode);
				Assert.AreEqual(expectedTokens, tokens.Count);
				Assert.AreEqual(token, tokens[expectedTokenIndex].ActualToken);
			}
		}
	}
}